//安装 npm i express
//导入 express
const express = require('express');
const {resolve} = require('path');
const qs = require('querystring');

//创建服务对象
const app = express();

//设置一个中间件
app.use((req, res, next) => {
  //声明一个字符串
  let str = '';
  //绑定事件
  req.on('data', chunk => {
    str += chunk;
  })

  req.on('end', () => {
    let data = qs.parse(str);
    //将 data 请求体对象数据, 存储在 req 身上
    req.body = data;
    //执行后续的路由回调
    next();
  })
});

//设置路由规则
app.get('/login', (req, res) => {
  //响应某个文件的内容
  res.sendFile(resolve(__dirname, './public/login.html'));
});

//路由规则
app.post('/login', (req, res) => {
  //3. 通过 req.body 获取请求体数据
  let {email, password} = req.body;

  console.log(email, password);
  res.send('登录成功');
});

//监听端口 启动服务
app.listen(80, () => {
  console.log('服务已经启动....');
})